package com.pagepub.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

public class MyUtil {

	
	/**
	 * 查询路径
	 * 
	 * @param request
	 *            响应
	 * @return 查询路径
	 */
	public static String getParams(HttpServletRequest request) {
		String totalParams = "";

		Enumeration params = request.getParameterNames();// 得到所有参数名
		while (params.hasMoreElements()) {

			String tempName = params.nextElement().toString(); // 取得元素名
			String tempValue = request.getParameter(tempName);
			if (tempName.equals("currentpage") || tempName.equals("method") || tempName.equals("1"))
				continue;
			if ("key".equals(tempName)) {
				try {
					tempValue = URLEncoder.encode(tempValue, "UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
			if (tempValue != null && !tempValue.equals("")
					&& !tempName.equals("currentPage")) {
				if ("".equals(totalParams)) {

					totalParams = totalParams + tempName + "=" + tempValue;
				} else {
					totalParams = totalParams + "&" + tempName + "="
							+ tempValue;
				}
			}
		}
		return totalParams;
	}
	
	/**
	 * 
	 * @Function:转换编码格式
	 * 
	 * @date :
	 * @author :
	 * @method : FormetFileSize
	 */
	 public static String getURLDecoder(String name) {
		 String result="";
		    try {
		    	result = name;
				//filename = new String(filename.getBytes("UTF-8"), "iso-8859-1");
		    	//result = URLEncoder.encode(name, "UTF-8");
		    	//result =new String(name.getBytes("UTF-8"),"iso-8859-1");   
		    	//result =URLDecoder.decode(name,"utf-8");
		    }
		    catch (Exception ex) {
		      return "";
		    }
		    return result;
	}
	
	/**
	 * 
	 * @Function:转换文件大小
	 * 
	 * @date :
	 * @author :
	 * @method : FormetFileSize
	 */
	public static String formatFileSize(float fileS) {
		DecimalFormat df = new DecimalFormat("#.00");
		String fileSizeString = null;
		if (fileS < 1024) {
			fileSizeString = df.format((double) fileS) + "B";
		} else if (fileS < 1048576) {
			fileSizeString = df.format((double) fileS / 1024) + "K";
		} else if (fileS < 1073741824) {
			fileSizeString = df.format((double) fileS / 1048576) + "M";
		} else {
			fileSizeString = df.format((double) fileS / 1073741824) + "G";
		}
		return fileSizeString;
	}

	/**
	 * 函数名称：arrayToStr 函数功能:把数组转换为字符串列表
	 * 
	 * @param array1
	 *            数组
	 * @return 用","隔开的字符串列表 字符上面加单引号
	 */
	public static String arrayToStr(String[] array1) {
		String sResult = "";
		if (array1 == null) {
			return sResult;
		}
		if(array1.length==1){
			for (int i = 0; i < array1.length; i++) {
				if ("".equals(sResult)) {
					sResult = "" + array1[i] + "";
				} else {
					sResult += "," + "" + array1[i] + "";
				}
			}
		}else{
			for (int i = 0; i < array1.length; i++) {
				if(i==0){
					if ("".equals(sResult)) {
						sResult = "" + array1[i] + "'";
					}
				}else if(i==(array1.length-1)){
					if (!"".equals(sResult)) {
						sResult += "," + "'" + array1[i] + "";
					}
				}else{
					if (!"".equals(sResult)) {
						sResult += "," + "'" + array1[i] + "'";
					}	
				}
				
			}
		}
		
		return sResult;
	}

	
	/**
	 * 函数名称：文件名称过长，进行截取
	 * 
	 * @param fileName
	 * @return
	 */
	public static String interceptString(String fileName){
		int num= fileName.indexOf("(");
		if(num>12){
			if(fileName.length()>16){
				fileName=fileName.substring(0, 16)+"...";
			}
			
		}else if(fileName.length()>26){
			fileName=fileName.substring(0, 26)+"...";
		}
		return fileName;
	}
	
	
	/**
	 * 
	 * @Function:取得文件夹大小
	 * 
	 * @date : 
	 * @author : 
	 * @method : getFileSize
	 */
	public static long getFileSize(File f) throws Exception// 取得文件夹大小
	{
		long size = 0;
		File flist[] = f.listFiles();
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getFileSize(flist[i]);
			} else {
				size = size + flist[i].length();
			}
		}
		return size;
	}

	/**
	 * 
	 * @Function:目录文件的个数
	 * 
	 * @date : 
	 * @author : 
	 * @method : getlist
	 */
	public static long getlist(File f) {// 递归求取目录文件个数
		long size = 0;
		File flist[] = f.listFiles();
		size = flist.length;
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getlist(flist[i]);
				size--;
			}
		}
		return size;
	}
	
	
	/**
	 * 
	 * @Function:毫秒 转换为 时、分、秒 格式
	 * 
	 * @date : 
	 * @author :
	 * @method : formatDuring
	 */
	public static String formatDuring(long mss) {
		StringBuilder time= new StringBuilder();
		long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		if(hours>0){
			time.append(hours+"小时");
		}
		if(minutes>0){
			time.append(minutes+"分钟");
		}
		if(seconds>0){
			time.append(seconds+"秒");
		}
		if(hours==0&&minutes==0&&seconds==0){
			time.append(mss+"毫秒...");
		}
		return time.toString();
	}
	
	/**
	 * 
	 * @Function:获取文件大小
	 * 
	 * @date : 
	 * @author :
	 * @method : getFileSizes
	 */
	public static long getFileSizes(File f) throws Exception {// 取得文件大小
		long s = 0;
		if (f.exists()) {
			FileInputStream fis = null;
			fis = new FileInputStream(f);
			s = fis.available();
		} else {
			f.createNewFile();
			System.out.println("文件不存在");
		}
		return s;
	}
}
